package com.itheima.dao;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserTest {
    private InputStream is;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    private List<User> list;
    private User user;
    private Integer result;

    @Test
    public void findAll() throws Exception{
        //1.加载核心配置文件
        is = UserTest.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSession工厂对象
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过SqlSession工厂对象获取SqlSession对象
        sqlSession = sqlSessionFactory.openSession();
        //4.执行映射配置文件中的sql语句，并接收结果
         list = sqlSession.selectList("user.findAll");
        //5.处理结果
        for (User user : list) {
            System.out.println(user);
        }
        //6.释放资源
        sqlSession.close();
        is.close();
    }

    @Test
    public void findById() throws Exception{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
             Integer id=2;
            user = sqlSession.selectOne("user.findById",id);
            //5.返回结果
            System.out.println(user);
            //6.释放资源
            sqlSession.close();
            is.close();
    }

    @Test
    public void insert()throws Exception {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            user = new  User(4,"anna","123","12311111111");
            result = sqlSession.insert("user.insert",user);
            //5.提交事务
            sqlSession.commit();
            //6.处理结果
            System.out.println(result);
            //7.释放资源
            sqlSession.close();
            is.close();
    }

    @Test
    public void update()throws Exception {
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            user = new User(4,"mark","123","12311111111");
            result = sqlSession.update("user.update",user);
            //5.提交事务
            sqlSession.commit();
            //6.处理结果
            System.out.println(result);
            //7.释放资源
            sqlSession.close();
            is.close();
    }

    @Test
    public void delete() throws Exception{
            //1.加载核心配置文件
            is = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSession工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            //3.通过工厂对象获取SqlSession对象
            sqlSession = sqlSessionFactory.openSession(true);
            //4.执行映射配置文件中的sql语句，并接收结果
            Integer id =3;
            result = sqlSession.delete("user.delete",id);
            //5.提交事务
            sqlSession.commit();
            //6.处理结果
            System.out.println(result);
            //7.释放资源
            sqlSession.close();
            is.close();
    }
}
